package cn.gailvlun.gll.presentation.chat.vm

import android.arch.lifecycle.ViewModel
import cn.gailvlun.gll.chatlib.db.ChatDB2
import cn.gailvlun.gll.chatlib.message.Message
import cn.gailvlun.gll.net.base.Profile
import io.realm.Realm


class ChatSettingViewModel: ViewModel {

    private var realm: Realm?

    constructor() : super() {
        realm = Realm.getInstance(ChatDB2.getConfiguration());
    }

    fun getProfile(userId: Int): Profile? {
        return realm!!.where(Profile::class.java).equalTo("id", userId).findFirst()
    }

    fun deleteAllMessage(dialogId: String) {
        realm!!.executeTransaction { realm ->
            realm.where(Message::class.java)
                    .equalTo("dialogId", dialogId)
                    .findAll()
                    .deleteAllFromRealm()
        }
    }

    override fun onCleared() {
        realm?.close()
        realm = null

        super.onCleared()
    }
}